import React from 'react';
import { Navigate } from 'react-router-dom';
import Layout from '../../components/Layout';
import bpmRoutes from './bpm';
import mallRoutes from './mall';
import crmRoutes from './crm';
import iotRoutes from './iot';
import { documentRoutes } from './document';

// 路由配置类型定义
export interface RouteConfig {
  path: string;
  element?: React.ReactNode;
  children?: RouteConfig[];
  meta?: {
    title?: string;
    icon?: string;
    hidden?: boolean;
    noCache?: boolean;
    affix?: boolean;
    noTagsView?: boolean;
    activeMenu?: string;
    canTo?: boolean;
    breadcrumb?: boolean;
  };
}

// 懒加载组件
const Home = React.lazy(() => import('../../views/Home'));
const Dashboard = React.lazy(() => import('../../views/Dashboard'));
const System = React.lazy(() => import('../../views/System'));
const Login = React.lazy(() => import('../../views/Login'));
const Profile = React.lazy(() => import('../../views/Profile'));
const UserCenter = React.lazy(() => import('../../views/UserCenter'));
const Error403 = React.lazy(() => import('../../views/Error/403'));
const Error404 = React.lazy(() => import('../../views/Error/404'));
const Error500 = React.lazy(() => import('../../views/Error/500'));
const Redirect = React.lazy(() => import('../../views/Redirect'));

/**
 * 路由配置说明:
 * meta: {
 *   hidden: true              当设置 true 的时候该路由不会再侧边栏出现 如404，login等页面(默认 false)
 *   title: 'title'            设置该路由在侧边栏和面包屑中展示的名字
 *   icon: 'icon-name'         设置该路由的图标
 *   noCache: true             如果设置为true，则不会被缓存(默认 false)
 *   breadcrumb: false         如果设置为false，则不会在breadcrumb面包屑中显示(默认 true)
 *   affix: true               如果设置为true，则会一直固定在tag项中(默认 false)
 *   noTagsView: true          如果设置为true，则不会出现在tag中(默认 false)
 *   activeMenu: '/dashboard'  显示高亮的路由路径
 *   canTo: true               设置为true即使hidden为true，也依然可以进行路由跳转(默认 false)
 * }
 */
const remainingRoutes: RouteConfig[] = [
  {
    path: '/redirect',
    element: <Layout />,
    children: [
      {
        path: '/redirect/:path',
        element: <Redirect />,
        meta: {
          hidden: true,
          noTagsView: true
        }
      }
    ],
    meta: {
      hidden: true,
      noTagsView: true
    }
  },
  {
    path: '/',
    element: <Layout />,
    children: [
      {
        path: '',
        element: <Navigate to="/index" replace />,
      },
      {
        path: 'index',
        element: <Home />,
        meta: {
          title: '首页',
          icon: 'home',
          noCache: false,
          affix: true
        }
      }
    ]
  },
  {
    path: '/dashboard',
    element: <Layout />,
    children: [
      {
        path: '',
        element: <Dashboard />,
        meta: {
          title: '仪表盘',
          icon: 'dashboard',
          noCache: false
        }
      }
    ]
  },
  {
    path: '/system',
    element: <Layout />,
    children: [
      {
        path: '',
        element: <System />,
        meta: {
          title: '系统设置',
          icon: 'setting',
          noCache: false
        }
      }
    ]
  },
  {
    path: '/user',
    element: <Layout />,
    children: [
      {
        path: 'profile',
        element: <Profile />,
        meta: {
          canTo: true,
          hidden: true,
          noTagsView: false,
          icon: 'user',
          title: '个人资料'
        }
      },
      {
        path: 'center',
        element: <UserCenter />,
        meta: {
          canTo: true,
          hidden: true,
          noTagsView: false,
          icon: 'setting',
          title: '个人中心'
        }
      }
    ],
    meta: {
      hidden: true
    }
  },
  // BPM模块路由
  ...bpmRoutes,
  // 商城模块路由
  ...mallRoutes,
  // 合并 CRM 模块路由
  ...crmRoutes,
  // 合并 IoT 模块路由
  ...iotRoutes,
  // 文档模块路由
  ...documentRoutes,
  {
    path: '/login',
    element: <Login />,
    meta: {
      hidden: true,
      title: '登录',
      noTagsView: true
    }
  },
  {
    path: '/403',
    element: <Error403 />,
    meta: {
      hidden: true,
      title: '403',
      noTagsView: true
    }
  },
  {
    path: '/404',
    element: <Error404 />,
    meta: {
      hidden: true,
      title: '404',
      noTagsView: true
    }
  },
  {
    path: '/500',
    element: <Error500 />,
    meta: {
      hidden: true,
      title: '500',
      noTagsView: true
    }
  },
  {
    path: '*',
    element: <Navigate to="/404" replace />,
    meta: {
      hidden: true,
      breadcrumb: false
    }
  }
];

export default remainingRoutes;